<template>
  <div class="layout-body">
    <el-container>
      <el-aside class="aside" :style="{ width: asideWidth + 'px' }">
        <div class="logo">
          <span v-if="!menuCollapse">小豪管理后台</span>
        </div>
        <div class="menu-panel">
          <el-menu :default-openeds="defaultOpeneds" :collapse-transition="false" :collapse="menuCollapse"
            class="el-menu-vertical-demo" default-active="2" background-color="#3d3c4a" text-color="#fff"
            active-text-color="#fff" router :defaultActive="defaultActive">
            <!-- 侧边栏渲染 不要使用div进行嵌套 而是要使用 el-menu-item-group -->
            <!-- 有子菜单 -->
            <el-menu-item-group v-for="(item, itemKey) in menuList" :key="itemKey">
              <el-sub-menu :index="item.path" v-if="item.children">
                <template #title>
                  <i :class="['iconfont', item.icon]"></i>
                  <span class="menu-name">{{ item.menuName }}</span>
                </template>
                <el-menu-item v-for="(subItem, subItemKey) in item.children" :key="subItemKey" :index="subItem.path">
                  <span class="menu-name">{{ subItem.menuName }}</span>
                </el-menu-item>
              </el-sub-menu>
              <!-- 没有子菜单 -->
              <el-menu-item :index="item.path" v-else>
                <template #title>
                  <i :class="['iconfont', item.icon]"></i>
                  <span class="menu-name">{{ item.menuName }}</span>
                </template>
              </el-menu-item>
            </el-menu-item-group>
            <!-- 侧边栏渲染结算 -->
          </el-menu>
        </div>
      </el-aside>
      <el-container>
        <el-header class="header">
          <div :class="['op-menu', 'iconfont', menuCollapse ? 'icon-expand' : 'icon-collapse']" @click="opMenu"></div>
          <div class="menu-bread">
            <el-breadcrumb>
              <template v-for="item in menuBreadCrumbList">
                <el-breadcrumb-item v-if="item.name">
                  {{ item.name }}
                </el-breadcrumb-item>
              </template>
            </el-breadcrumb>
          </div>
        </el-header>
        <el-main class="main-content">
          <!-- 标签 -->
          <div class="tag-content">
            <el-tabs type="border-card" v-model="defaultActive" @tab-change="tabClick" @edit="editTab">
              <el-tab-pane v-for="item in tabList" :key="item.path" :name="item.path" :label="item.menuName"
                :closable="tabList.length > 1"></el-tab-pane>
            </el-tabs>
          </div>
          <div class="content-body">
            <router-view></router-view>
          </div>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script setup>
import { ref, getCurrentInstance, watch } from 'vue';
import { useRouter, useRoute } from 'vue-router'
const { proxy } = getCurrentInstance()
const router = useRouter()
const route = useRoute()

//侧边栏宽度
const asideWidth = ref(250)

// 默认展开的菜单
const defaultOpeneds = ref([])

// 默认激活的菜单
const defaultActive = ref()

//菜单对象
const menuMap = {}

//自定义侧边栏菜单数据
const menuList = [
  {
    menuName: "内容管理",
    icon: "icon-article",
    path: "/forum",
    children: [
      {
        menuName: "帖子管理",
        icon: "icon-article",
        path: "/forum/article",
      },
      {
        menuName: "评论管理",
        icon: "icon-article",
        path: "/forum/comment",
      },
      {
        menuName: "板块管理",
        icon: "icon-article",
        path: "/forum/board",
      },
    ],
  },
  {
    menuName: "用户管理",
    icon: "icon-user",
    path: "/user",
    children: [
      {
        menuName: "用户列表",
        icon: "icon-article",
        path: "/user/list",
      },
    ],
  },
  {
    menuName: "设置",
    icon: "icon-settings",
    path: "/settings",
    children: [
      {
        menuName: "系统设置",
        icon: "icon-article",
        path: "/settings/sys",
      },
    ],
  },
]


//初始化的时候展开侧边栏
const init = () => {
  menuList.forEach(item => {
    defaultOpeneds.value.push(item.path)
    //将子菜单存入menuMap中
    item.children.forEach(subItem => {
      menuMap[subItem.path] = subItem
    })
  })
}
init()



// 收起关闭菜单
const menuCollapse = ref(false)

// tab操作
const tabList = ref([])
//tab变化触发的方法
const tabClick = (e) => {
  //这里的e就是将tab的name传过来
  router.push(e)
}

/**
 * 关闭标签页
 * @param targetKey 标签页传递过来的name值 （路由路径）
 * @param action 执行的操作
 */
const editTab = (targetKey, action) => {
  if (action !== 'remove') {
    return
  }

  //当前的路径
  let currentPath = defaultActive.value

  let tabs = tabList.value
  //关闭当前选中的tab
  if (targetKey == defaultActive.value) {
    tabs.forEach((tab, index) => {
      if (tab.path == targetKey) {
        let nextTab = tabs[index + 1] || tabs[index - 1]
        //取到了nexttab
        if (nextTab) {
          currentPath = nextTab.path
        }
      }
    })
  }

  //删除之后更新路由
  tabList.value = tabs.filter(tab => {
    return tab.path !== targetKey
  })

  //当前路由不切换
  if (currentPath !== defaultActive.value) {
    router.push(currentPath)
  }
}

//面包屑导航
const menuBreadCrumbList = ref([])

//点击展开关闭侧边栏菜单
const opMenu = () => {
  menuCollapse.value = !menuCollapse.value
  if (menuCollapse.value) {
    asideWidth.value = 63
  } else {
    asideWidth.value = 250
  }
}

//监听默认激活的菜单并选中
watch(() => route, (newVal, oldVal) => {
  defaultActive.value = route.path
  //面包屑赋值
  menuBreadCrumbList.value = route.matched //  route.matched 匹配路由数据每一级的路由都可以获取 

  //判断当前路由是否在tab中存在，不存在则添加
  let currentMenu = tabList.value.find((item) => {
    return item.path == defaultActive.value
  })

  /*   let have = tabList.value.some((item) => {
      return item.path == defaultActive.value
    }) */

  if (!currentMenu) {
    //添加tab标签
    tabList.value.push(menuMap[route.path])
  }

}, { deep: true, immediate: true })


</script>

<style scoped lang="scss">
.el-popper {
  border: none !important;

  .el-menu-item.is-active {
    background: var(--el-color-primary);
  }

  .el-menu-item:hover {
    color: #d8d8ee;
  }

  .el-menu--popup {
    padding: 0px;
  }
}

.layout-body {
  .aside {
    background: #3d3c4a;

    .logo {
      display: flex;
      height: 50px;
      color: #fff;
      background: #191828;
      align-items: center;
      font-size: 18px;
      padding-left: 5px;
    }

    .menu-panel {
      height: calc(100vh - 50px);

      .menu-name {
        padding-left: 10px;
      }

      //去除边框
      .el-menu {
        border-right: none;
      }

      // 每个菜单的颜色
      .el-menu-item {
        background: #353544;
      }

      .el-menu-item.is-active {
        background: #198bee;
      }

      .el-menu-item:hover {
        color: #d8d8ee;
      }
    }
  }

  .header {
    background: #fff;
    border-bottom: 1px solid #ddd;
    height: 50px;
    line-height: 50px;
    padding: 0px 10px !important;
    display: flex;
    align-items: center;

    .op-menu {
      font-size: 20px;
      cursor: pointer;
      color: #3a3a40;
    }

    .menu-bread {
      margin-left: 10px;
    }
  }

  .main-content {
    padding: 0px;

    .tag-content {
      .el-tabs--border-card {
        border: none;
      }

      .el-tabs__content {
        display: none;
      }
    }

    .content-body {
      overflow: hidden;
      padding: 10px 10px 5px 10px;
    }
  }
}
</style>